# -*- coding: utf-8 -*-
import scrapy


class BaiduSpider(scrapy.Spider):
    name = 'baidu'
    allowed_domains = ['barefeet.cn']
    start_urls = ['https://barefeet.cn/?id=dianying']
    with open('barefeet.cn.html', 'w') as f:
        f.write('------------')

    def parse(self, response):
        for quote in response.xpath('//div[@class="col-md-2 col-sm-3 col-xs-4"]'):
            title = quote.xpath(
                "./div[@class='title']//a/text()").extract_first()
            image = quote.xpath(
                "//a[@class='videopic lazy']/@data-original").extract_first()
            url = quote.xpath(
                "//a[@class='videopic lazy']/@href").extract_first()
            yield{
                'title': (title),
                'image': (image),
                'url': (url)
            }
            with open('barefeet.cn.html', 'a') as f:
                f.write(title+' '+image+' '+url + '\r\n')
        next_page = response.xpath(
            '//div//div[@class="ew-page"]//li[last()]/a/@href').extract_first()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            # next_page = 'https://barefeet.cn/?id=dianying&cat=all&area=all&year=all&act=all&rank=rankhot&page='+str(page)
            print(next_page)
            yield response.follow(next_page, callback=self.parse)
            # yield scrapy.Request(next_page, callback=self.parse, dont_filter=True)

            # with open('barefeet.cn.html', 'a') as f:
            #     f.write(title+' '+image+' '+url + '\r\n')
